package com.ruoyi.OnePipe;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.extdata.common.ExtReqEnum;
import com.ruoyi.extdata.common.OnepipeTripleDES;
import com.ruoyi.extdata.domain.OkRequestVo;
import okhttp3.*;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class OnePipeUtils {

    private static final String apiKey = "7kxNRvNQ44F2GIhbSgMa_93b6a81505ee47ab8f58927b8928afe9";
    private static final String secretKey = "GG3zwpPhNC9sq67T";

    private static final String auth_provider = "QuickTeller";

    public static void main(String[] args) {
        String serialNo = IdUtils.reqId();
        System.out.println("serialNo：" + serialNo);
        String accountNumber = "0016588667";
        String bankCode = "058";
        String toBeEncryted = accountNumber + ";" + bankCode;

        String md5str = Hex.encodeHexString(DigestUtils.getMd5Digest().digest((serialNo + ";" + secretKey).getBytes()));
        System.out.println("signature==========：" + md5str);
        String secure = OnepipeTripleDES.encrypt(toBeEncryted, secretKey);
        System.out.println("secure：" + secure);

        Headers.Builder headersBuilder = new Headers.Builder();
        headersBuilder.add("Content-Type", "application/json");
        headersBuilder.add("Authorization", "Bearer " + apiKey);
        headersBuilder.add("Signature", md5str);
        Headers headers = headersBuilder.build();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("request_ref", serialNo);
        jsonObject.put("request_type", "lookup_account_mid");
        JSONObject auth = new JSONObject();
        auth.put("type", "bank.account");
        auth.put("secure", secure);
        auth.put("auth_provider", "QuickTeller");
        jsonObject.put("auth", auth);
        JSONObject transaction = new JSONObject();
        transaction.put("transaction_ref", serialNo);
        transaction.put("transaction_desc", "kyc");
        JSONObject customer = new JSONObject();
        customer.put("customer_ref", serialNo);
        transaction.put("customer", customer);
        jsonObject.put("transaction", transaction);

        OkRequestVo okRequestVo = OkRequestVo.builder().apiName("Lookup-Account-Mid").url("https://api.onepipe.io/v2/transact").reqMethod("POST").params(jsonObject).reqEnum(ExtReqEnum.JSON).headers(headers).build();

        MediaType mediaType = MediaType.parse(okRequestVo.getReqEnum().getType());
        RequestBody body = RequestBody.create(mediaType, okRequestVo.getParams().toString());

        Request request = new Request.Builder()
                .url(okRequestVo.getUrl())
                .method(okRequestVo.getReqMethod(), body)
                .headers(okRequestVo.getHeaders())
                .build();
        OkHttpClient client = new OkHttpClient().newBuilder()
                //设置连接超时时间
                .connectTimeout(160, TimeUnit.SECONDS)
                //设置读取超时时间
                .readTimeout(160, TimeUnit.SECONDS)
                .build();

        try {
            Response response = client.newCall(request).execute();
            System.err.println("response1:" + response.body().toString());
            System.err.println("response2:" + response.body().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static JSONObject getData(String refNo, String accountNumber, String bankCode) throws Exception {
        String md5str = Hex.encodeHexString(DigestUtils.getMd5Digest().digest((refNo + ";" + secretKey).getBytes()));

        OkHttpClient client = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/json");

        JSONObject transaction = new JSONObject();
        transaction.put("account_number", accountNumber);
        transaction.put("bank_code", bankCode);
        transaction.put("customer", new JSONObject());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("request_ref", refNo);
        jsonObject.put("transaction", transaction);
        RequestBody body = RequestBody.create(mediaType, jsonObject.toString());
        Request request = new Request.Builder()
                .url("https://api.onepipe.io/v1/lookup/accountinfo")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer " + apiKey)
                .addHeader("Signature", md5str)
                .addHeader("Timestamp", "1630403646")
                .build();
        Response response = client.newCall(request).execute();



        String string = response.body().string();
        JSONObject jsonObject1 = JSONObject.parseObject(string);
        if ("Successful".equals(jsonObject1.get("status")) && "Successful".equals(jsonObject1.get("message"))) {
            return jsonObject1.getJSONObject("data").getJSONObject("account_info");
        } else {
            return null;
        }
    }


    public static JSONObject getData2(String refNo, String accountNumber, String bankCode) throws Exception {
        String md5str = Hex.encodeHexString(DigestUtils.getMd5Digest().digest((refNo + ";" + secretKey).getBytes()));

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        JSONObject transaction = new JSONObject();
        transaction.put("account_number", accountNumber);
        transaction.put("bank_code", bankCode);
        transaction.put("customer", new JSONObject());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("request_ref", refNo);
        jsonObject.put("transaction", transaction);


        RequestBody body = RequestBody.create(mediaType, jsonObject.toString());
        Request request = new Request.Builder()
                .url("https://api.onepipe.io/v2/transact  ")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer " + apiKey)
                .addHeader("Signature", md5str)
                .addHeader("Timestamp", "1630403646")
                .build();
        Response response = client.newCall(request).execute();

        String string = response.body().string();
        JSONObject jsonObject1 = JSONObject.parseObject(string);
        if ("Successful".equals(jsonObject1.get("status")) && "Successful".equals(jsonObject1.get("message"))) {
            return jsonObject1.getJSONObject("data").getJSONObject("account_info");
        } else {
            return null;
        }
    }


}
